import { createRouter, createWebHistory } from 'vue-router';
import Home from '../views/Home.vue';
import Layout from '@/components/Layout.vue';
import Messages from '../views/Messages.vue';
import Profile from '../views/Profile.vue';
import ChangePassword from '../views/ChangePassword.vue';
import Login from '../views/auth/login.vue';
import Register from '../views/auth/Register.vue';
import ForgotPassword from '../views/auth/ForgotPassword.vue';
import cj from '../views/cj.vue';

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Layout,
    children: [
      {
        path: '', // 默认子路由
        name: 'Default',
        component: Home,
        meta: { requiresAuth: true }
      },
      {
        path: 'home',
        name: 'HomeView',
        component: Home,
        meta: { requiresAuth: true }
      },
      {
        path: 'messages',
        name: 'Messages',
        component: Messages,
        meta: { requiresAuth: true }
      },
      {
        path: 'profile',
        name: 'Profile',
        component: Profile,
        meta: { requiresAuth: true }
      },
      {
        path: 'change-password',
        name: 'ChangePassword',
        component: ChangePassword,
        meta: { requiresAuth: true }
      },
      {
        path: 'cj',
        name: 'cj',
        component: () => import('@/views/cj.vue'),
        meta: { requiresAuth: true }
      },
      {
        path: 'friends',
        name: 'Friends',
        component: () => import('../views/friends/Friends.vue'),
        meta: { requiresAuth: true }
      },
      {
        path: 'friends/requests',
        name: 'FriendRequests',
        component: () => import('../views/friends/FriendRequests.vue'),
        meta: { requiresAuth: true }
      },
      {
        path: 'groups',
        name: 'Groups',
        component: () => import('../views/groups/Groups.vue'),
        meta: { requiresAuth: true }
      },
      {
        path: 'rooms',
        name: 'Rooms',
        component: () => import('../views/rooms/Rooms.vue'),
        meta: { requiresAuth: true }
      },
      {
        path: 'settings',
        name: 'Settings',
        component: () => import('../views/settings/Settings.vue'),
        meta: { requiresAuth: true }
      },
      {
        path: 'private-chat/:id',
        name: 'PrivateChat',
        component: () => import('../views/chat/PrivateChat.vue'),
        meta: { requiresAuth: true }
      },
      {
        path: 'group-chat/:id',
        name: 'GroupChat',
        component: () => import('../views/chat/GroupChat.vue'),
        meta: { requiresAuth: true }
      },
      {
        path: 'room-chat/:id',
        name: 'RoomChat',
        component: () => import('../views/chat/RoomChat.vue'),
        meta: { requiresAuth: true }
      }
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: { requiresAuth: false }
  },
  {
    path: '/register',
    name: 'Register',
    component: Register,
    meta: { requiresAuth: false }
  },
  {
    path: '/forgot-password',
    name: 'ForgotPassword',
    component: ForgotPassword,
    meta: { requiresAuth: false }
  }
];

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
});

// 路由守卫
router.beforeEach((to, from, next) => {
  const isAuthenticated = localStorage.getItem('token')
  
  if (to.path === '/login' && isAuthenticated) {
    // 如果已登录且访问登录页，则重定向到主页
    next('/home')
  } else if (to.path !== '/login' && !isAuthenticated) {
    // 如果未登录且访问非登录页，则重定向到登录页
    next({
      path: '/login',
      query: { redirect: to.fullPath }
    })
  } else {
    next()
  }
});

export default router;